import {RouteRecordRaw} from "vue-router"
import {ElMessage} from "element-plus"

/**
 * 处理文件名称
 */
const defaultGetName = (modelFileName: any) => {
    let registerModelName = modelFileName

    try {
        const matchNameResult = modelFileName.match(/.\/(.+?)\./)
        if (matchNameResult) {
            registerModelName = matchNameResult[1]
        } else {
            registerModelName = modelFileName.split('./')[1].split('.')[0]
        }
    } catch (e) {
        console.log("菜单未加载成功，错误信息：" + e)
        ElMessage.error("菜单未加载成功，请联系管理员")
    }
    return registerModelName
}

/**
 * 拼接导出的文件成数组
 */
const importMetaGlobDefaultContentTransfer = (modules: Record<string, any>, getName = defaultGetName) => {
    return Object.keys(modules).reduce((memo, moduleFileName) => {
        const moduleContent: any = modules[moduleFileName].default
        if (moduleContent) {
            const registerModuleName = getName(moduleFileName)
            memo[registerModuleName] = moduleContent
        }
        return memo
    }, {} as Record<string, any>)
}

/**
 * 导入模块并生成路由数组
 */
const importRoutes = import.meta.glob('./***/*.ts', {
    eager: true
})
const routes = Object.values(importMetaGlobDefaultContentTransfer(importRoutes)).flatMap(item => item)

/**
 * 路由
 */
const routerList: RouteRecordRaw[] = [
    /**
     * 系统
     */
    //基础信息
    {
        path: "/menu-setting",
        name: "menu-setting",
        component: () => import("@/pages/system/menu-setting/index.vue"),
        meta: {
            title: "菜单管理"
        }
    },
    /**
     * 博客
     */
    {
        path: "/blog-categories",
        name: "blog-categories",
        component: () => import("@/pages/blog/blog-categories/index.vue"),
        meta: {
            title: "博客分类"
        }
    },
    {
        path: "/blog-texts",
        name: "blog-texts",
        component: () => import("@/pages/blog/blog-texts/index.vue"),
        meta: {
            title: "博客文章"
        }
    },
    {
        path: "/blog-setting",
        name: "blog-setting",
        component: () => import("@/pages/blog/blog-setting/index.vue"),
        meta: {
            title: "博客全局配置"
        }
    }
]

export default routerList